package com.example.view.activity;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.drawable.AnimationDrawable;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.common.base.AbsActivity;
import com.example.view.R;
import com.example.view.databinding.ActAnimationBinding;

/**
 *
 */
@Route(path = "/view/animation")
public class AnimationActivity extends AbsActivity<ActAnimationBinding> {

    @Override
    protected int getLayoutId() {
        return R.layout.act_animation;
    }

    @Override
    protected void main() {
        AnimationDrawable animation = (AnimationDrawable) mBinding.ivFrameAnim.getDrawable();
        animation.start();
    }

    @Override
    protected void initListener() {

        mBinding.btTran.setOnClickListener(v -> {
            ObjectAnimator anim = ObjectAnimator.ofFloat(mBinding.btTran, "TranslationZ", 0, 30,60, 120, 180, 360, 450, 600, 540, 600);
            anim.setDuration(2000);
            anim.start();
        });

        mBinding.btRotate.setOnClickListener(v -> {
            ValueAnimator anim = ValueAnimator.ofFloat(0, 180, 360, 180, 0);
            anim.setDuration(2000);
            anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    Float value = (Float) animation.getAnimatedValue();
                    mBinding.btRotate.setRotation(value);
                    mBinding.btTran.setRotation(value);
                    mBinding.btScale.setRotation(value);

                }
            });
            anim.start();
        });

        mBinding.btScale.setOnClickListener(v -> {
            ObjectAnimator anim = ObjectAnimator.ofFloat(mBinding.btScale, "ScaleX", 1, 0, -1, 0, 1);
            anim.setDuration(2000);
            anim.start();
        });

        mBinding.btAlpha.setOnClickListener(v -> {
            ObjectAnimator anim = ObjectAnimator.ofFloat(mBinding.btAlpha, "alpha", 1, 0, 1);
            anim.setDuration(4000);
            anim.start();
        });

        mBinding.ivFrameAnim.setOnClickListener(v -> {
            TranslateAnimation anim1 = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_PARENT, -0.8F, TranslateAnimation.RELATIVE_TO_PARENT, 0.8F,
                    TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_PARENT, 0);
            // 时间
            anim1.setDuration(2000);
            // 结束时保持(动画结束后的)状态
            anim1.setFillAfter(true);
            anim1.setRepeatMode(Animation.RESTART);
            anim1.setRepeatCount(Integer.MAX_VALUE);

            AlphaAnimation anim2 = new AlphaAnimation(1, 0);
            // 时间
            anim2.setDuration(1500);
            // 结束时保持(动画结束后的)状态
            anim2.setFillAfter(true);
            anim2.setStartOffset(500);
            anim2.setRepeatMode(Animation.RESTART);
            anim2.setRepeatCount(-1);

            AnimationSet anims = new AnimationSet(true);
            anims.addAnimation(anim1);
            anims.addAnimation(anim2);

            mBinding.ivFrameAnim.startAnimation(anims);
        });
    }
}
